package com.island.gomall.controller;

import com.island.gomall.common.util.R;
import com.island.gomall.entity.CategoryEntity;
import com.island.gomall.service.CategoryService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/product/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 树形查
     * @return
     */
    @RequestMapping(value = "/listWithTree")
    public R listCategory(){
        List<CategoryEntity> categoryEntityList = categoryService.getCategoryWithTree();
        return R.ok().put("category",categoryEntityList);
    }
    @RequestMapping(value = "/addCategoryNode")
    public R addCategoryNode(@RequestParam("parentId") int parentId, @RequestParam("name") String name){
        categoryService.addCategoryNode(parentId, name);
        return R.ok();
    }

    @RequestMapping(value = "/deleteNode")
    public R removeCategoryNode(@RequestParam("id") int cid){
        if(categoryService.removeCategoryNode(cid)){
            return R.ok();
        } else {
            return R.error().put("msg","该节点存在子节点");
        }

    }
}
